<?php
declare (strict_types = 1);

namespace app\api\controller;

use think\facade\Db;
use app\BaseController;

class Index extends BaseController
{
    public function jobclass()
    {
    	$list = Db::name('jobclass')->cache(7200)->select();

        return json(['status'=>200,'data'=>$list]);
    }

    public function getaddress($location)
    {
        $url= "https://apis.map.qq.com/ws/geocoder/v1";
        $data= ['key' => 'S5FBZ-M5H6U-WRVVI-23BT3-CBNRE-B7BQM','location' => $location ];
        $method=isset($param['method'])?$param['method']:'post';
        //初使化init方法
        $ch = curl_init();
    
        //指定URL
        curl_setopt($ch, CURLOPT_URL, $url);
    
        //设定请求后返回结果
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
        if($method=='post'){
            //声明使用POST方式来进行发送  注释这个就是get请求
            curl_setopt($ch, CURLOPT_POST, 1);
        }
    
        //发送什么数据呢
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
        //忽略证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    
        //忽略header头信息
        curl_setopt($ch, CURLOPT_HEADER, 0);
    
        //设置超时时间
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    
        //发送请求
        $output = curl_exec($ch);
    
        //关闭curl
        curl_close($ch);
        
        return $output;
    }

    /**
     * 获取位置信息
     * */
    public function getcode()
    {
        $data = $this->_vali([
            'longitude.require'     => 'longitude不能为空',
            'latitude.require'     => 'latitude不能为空',
        ],'POST');
                
        
        $address = $this->getaddress($data['latitude'].','.$data['longitude']);
        
        $address = json_decode($address,true);
        
        return json(['status'=>200,'address' => $address['result']['address'] ]);
    }

    /**
     * 获取地址
     * @return \think\response\Json
     */
    public function getcity()
    {
        $data = $this->_vali([
            'longitude.require'     => 'longitude不能为空',
            'latitude.require'     => 'latitude不能为空',
        ],'POST');

        $address = $this->getaddress($data['latitude'].','.$data['longitude']);

        $address = json_decode($address,true);

        return json(['status'=>200,'address' => $address['result']['address_component'] ]);

    }
}
